package com.lenovo.manufacture.module;

import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;

import androidx.appcompat.app.AppCompatActivity;

import com.beardedhen.androidbootstrap.BootstrapLabel;
import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.github.mikephil.charting.formatter.ValueFormatter;
import com.lenovo.manufacture.R;
import com.lenovo.manufacture.net.BaseActivity;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

public class module11 extends BaseActivity {

    @BindView(R.id.tv2)
    BootstrapLabel tv2;
    @BindView(R.id.tv1)
    BootstrapLabel tv1;
    @BindView(R.id.chart)
    LineChart chart;
    Handler handler = new Handler();
    Runnable r = new Runnable() {
        @Override
        public void run() {
            init();
        }
    };
    List<Entry> entries1;
    List<Entry> entries2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_module11);
        ButterKnife.bind(this);
        setChart();
        init();

    }

    private void setChart() {
        entries1 = new ArrayList<>();
        entries2 = new ArrayList<>();
        chart.getXAxis().setPosition(XAxis.XAxisPosition.BOTTOM);
        chart.getAxisLeft().setAxisMaximum(40);
        chart.getAxisLeft().setAxisMinimum(-10);
        chart.setScaleEnabled(false);
        chart.setExtraOffsets(100, 10, 0, 50);
        chart.getAxisRight().setDrawAxisLine(false);
        chart.getXAxis().setValueFormatter(new ValueFormatter() {
            @Override
            public String getFormattedValue(float value) {
                return new Date(((long) value) * 1000).getHours() + ":00";
            }
        });
    }

    private void setData() {
        LineDataSet lineDataSet1 = new LineDataSet(entries1, "室外温度");
        LineDataSet lineDataSet2 = new LineDataSet(entries2, "车间温度");
        lineDataSet1.setLineWidth(5);
        lineDataSet2.setLineWidth(5);
        lineDataSet1.setColor(Color.parseColor("#FF5B5B"));
        lineDataSet2.setColor(Color.parseColor("#82AAFF"));
        LineData lineData = new LineData(lineDataSet1, lineDataSet2);
        chart.setData(lineData);
    }

    private void init() {
        async(data.FactoryInfo(t.test("id", 1).b()), v -> {
            String s1 = v.getData().get(0).get("outdoorTemp");
            String s2 = v.getData().get(0).get("workshopTemp");
            float time = Float.parseFloat(v.getData().get(0).get("time"));
            tv1.setText("环境温度\n" + s1 + "℃");
            tv2.setText("车间温度\n" + s2 + "℃");
            if (entries1.size() > 24) {
                chart.getLineData().getDataSetByIndex(0).removeFirst();
                chart.getLineData().getDataSetByIndex(1).removeFirst();
            }
            entries1.add(new Entry(time, Float.parseFloat(s1)));
            entries2.add(new Entry(time, Float.parseFloat(s2)));
            setData();
            chart.invalidate();
            handler.postDelayed(r, 2000);
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        handler.removeCallbacks(r);
    }
}